package com.skyable.account.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skyable.account.entity.SysRole;
import com.skyable.account.service.IRoleService;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.common.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GaoYuan
 * @title: RoleController
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1717:23
 */
@Api(value = "角色管理")
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    /**
     * 新增角色信息
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "新增角色")
    @PostMapping("/insert")
    public ResponseResult insertRole(@RequestBody SysRole sysRole, HttpServletRequest request) {
        setInsertInfo(sysRole,request);
        ServiceResult<SysRole> serviceResult = roleService.insertRole(sysRole,sysRole.getPermissionIdList());
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 更新角色信息
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "更新角色")
    @PutMapping("/update")
    public ResponseResult updateRole(@RequestBody SysRole sysRole, HttpServletRequest request) {
        setUpdateInfo(sysRole,request);
        ServiceResult<SysRole> serviceResult = roleService.updateRole(sysRole,sysRole.getPermissionIdList());
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 获取角色列表
     * @return
     */
    @ApiOperation(value = "获取角色列表")
    @GetMapping("/view")
    public ResponseResult sysRoleList(){
        ServiceResult<List<SysRole>> serviceResult = roleService.findRoleList();
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 分页查询角色列表
     * @param page
     * @param limit
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "分页查询角色列表")
    @PostMapping("/view/{page}/{limit}")
    public ResponseResult sysRoleList(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit, @RequestBody SysRole sysRole){
        ServiceResult<IPage<SysRole>> serviceResult = roleService.findRoleListByPage(page,limit,sysRole);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult().getRecords());
    }

    /**
     * 根据ID查询角色及权限信息
     * @param roleId
     * @return
     */
    @ApiOperation(value = "查询角色信息")
    @GetMapping("/view/{id}")
    public ResponseResult getSysRole(@PathVariable("id") Long roleId){
        ServiceResult<SysRole> serviceResult = roleService.findRoleById(roleId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deletePermission(@PathVariable("id") Long roleId){
        ServiceResult<Integer> serviceResult = roleService.deleteRoleById(roleId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }
}
